<?php
class Product
{
    private $CI;

    public $id;
    public $name;
    public $description;
    public $image;
    public $netto_price;
    public $price;
    public $profit_key;
    public $category_id;
    public $isFeatured;

    public $o_cat_name;

    function __construct($ids) {
        $this->CI =& get_instance();
    }

    public function update() {
        $data = array(
            'name' => $this->name,
            'description' => $this->description,
            'image' => $this->image,
            'netto_price' => $this->netto_price,
            'price' => $this->price,
            'profit_key' => $this->profit_key,
            'category_id' => $this->category_id,
            'isFeatured' => $this->isFeatured
        );

        $this->CI->db->where('id', $this->id);
        $res = $this->CI->db->update(TABLE_IMAGES, $data);

        if (!$res) {
            $msg = $this->db->_error_message();
            KLogger::Instance()->logDebug("Products->update() sql error: " . $msg);
        }
    }

    public function delete() {
        $res = $this->CI->db->delete(TABLE_IMAGES, array('id' => $this->id));

        if (!$res) {
            $msg = $this->db->_error_message();
            //$num = $this->db->_error_number();

            KLogger::Instance()->logDebug("Products->delete() sql error: " . $msg);
        }
    }

    public function upload() {
        $data = array(
            'name' => $this->name,
            'description' => $this->description,
            'image' => $this->image,
            'netto_price' => $this->netto_price,
            'price' => $this->price,
            'profit_key' => $this->profit_key,
            'category_id' => $this->category_id,
            'isFeatured' => $this->isFeatured
        );

        $this->CI->db->insert(TABLE_IMAGES, $data);
    }

    public function toJSON() {
        return json_encode($this);
    }
}
?>